/*
 * @Author: saber
 * @Date: 2021-11-02 15:35:55
 * @LastEditTime: 2021-11-02 18:03:15
 * @LastEditors: saber
 * @Description:
 */
import custom_http from './saber-http/custom-axios';

const http = custom_http;
export const getUrl = (path: string, queryParams: any, _method: string) => {
  const params = {
    ...(queryParams || ({} as any)),
  };

  const url = path.replace(/\{([^\\}]*(?:\\.[^\\}]*)*)\}/gm, (match, key) => {
    // eslint-disable-next-line no-param-reassign
    key = key.trim();

    if (params[key] !== undefined) {
      const value = params[key];
      delete params[key];
      return value;
    }
    console.warn('Please set value for template key: ', key);
    return '';
  });

  const paramStr = Object.keys(params)
    .map((key) => {
      return params[key] === undefined ? '' : `${key}=${params[key]}`;
    })
    .filter((id) => id)
    .join('&');

  if (paramStr) {
    return `${url}?${paramStr}`;
  }

  return url;
};
export default http;
